#include "stdafx.h"
#include "StringAlgorithm.h"
#include "Utility.h"

char* ReverseSentence(char *pData){
	if(pData == NULL)
		return NULL;
	char *pBegin = pData;
	char *pEnd = pData;
	while(*pEnd != '\0'){
		pEnd++;
	}
	pEnd--;
	Reverse(pBegin,pEnd);
	pBegin = pEnd = pData;
	while(*pBegin != '\0'){
		if(*pBegin == ' '){
			pBegin++;
			pEnd++;
		}else if(*pEnd == ' '|| *pEnd =='\0'){
			Reverse(pBegin,--pEnd);
			pBegin = ++pEnd;
		}else{
			pEnd++;
		}
	}
	return pData;
}


char* LeftRotateString(char *pStr, int n){
	int length = strlen(pStr);
	if(n > length)
		return pStr;
	int index1 = 0;
	int index2 = n-1;
	int index3 = n;
	int index4 = length - 1;
	Reverse(pStr,pStr+index2);
	Reverse(pStr+index3,pStr+index4);
	Reverse(pStr,pStr+index4);
	return pStr;
}

void ReplaceBlank(char str[], int length){
	if(str == NULL || length <= 0)
		return;
	char *p = str;
	int num = 0;
	while(*p != '\0'){
		if(*p++ == ' ')
			num++;
	}
	int newLength = strlen(str) + 2 * num + 1;
	if(newLength > length)
		return;
	int p1 = strlen(str) + 1;;
	int p2 = newLength;
	while( p1 >=0 && p2 > p1){
		if(str[p1] == ' '){
			str[p2--] = '0';
			str[p2--] = '2';
			str[p2--] = '%';
		}else{
			str[p2--] = str[p1];
		}
		p1--;
	}

}

